package com.jwdf.uda.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.View;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.bean.UploadImage;
import com.jwdf.uda.event.NewProblemsEvent;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.ui.fragment.question.EditProblemsFragment;
import com.jwdf.uda.ui.fragment.question.bean.EditDescribe;
import com.jwdf.uda.ui.fragment.question.bean.NewProblems;
import com.jwdf.uda.ui.fragment.question.bean.NewVote;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.UdaUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类说明：填写问题
 * Created by Administrator on 2016/9/1.
 */
public class EditProblemsActivity extends AbsTitleBarActivity {

    public static void start(Context context) {
        Intent intent = new Intent(context, EditProblemsActivity.class);
        context.startActivity(intent);
    }

    public static void start(Context context, PbBean.PbResQuestionFindQuestion questionFindQuestion) {
        NewProblems newProblems = onParsingData(questionFindQuestion);
        Intent intent = new Intent(context, EditProblemsActivity.class);
        Bundle mBundle = new Bundle();
        mBundle.putSerializable("NewProblems", newProblems);
        intent.putExtras(mBundle);
        context.startActivity(intent);

    }

    public static NewProblems onParsingData(PbBean.PbResQuestionFindQuestion questionFindQuestion) {
        final PbBean.PbBaseQuestion questionInfo = questionFindQuestion.getQuestionInfo();
        NewProblems.getInstance().setmNick(questionInfo.getTitle());
        NewProblems.getInstance().setNewVoteList(onParsingVoteList(questionInfo.getVoteOptionList()));
        NewProblems.getInstance().setmDescribe(onDescribe(questionInfo.getDescriptionList()));
        NewProblems.getInstance().setEditDescribeList(onEditDescribeList(questionInfo.getDescriptionList()));
        NewProblems.getInstance().setQuestionId(questionInfo.getQuestionId());
//        NewProblems.getInstance().setTagMap(questionInfo.getCategoryInfo());
//        NewProblems.getInstance().getIntTag(questionInfo.getCategoryInfo().getCategoryCode());
        NewProblems.getInstance().setTag(questionInfo.getCategoryInfo().getCategoryCode()-1);
        NewProblems.getInstance().setImageList(onImageList(questionInfo.getDescriptionList()));
        NewProblems.getInstance().setURLToken(onMapURLToken(questionInfo.getDescriptionList()));
        return NewProblems.getInstance();
    }
    public static List<NewVote> onParsingVoteList(List<PbBean.PbBaseResVoteOption> voteOption) {
        List<NewVote> newVoteLists = new ArrayList<>();
        for(int i = 0; i < voteOption.size(); i++) {
            NewVote newVote = new NewVote();
            newVote.setDescription(voteOption.get(i).getDescription());
            List<UploadImage> list = new ArrayList<>();
            for(int j = 0; j < voteOption.get(i).getOptionImageList().size(); j++) {
                UploadImage uploadImage = new UploadImage(voteOption.get(i).getOptionImage(j).getUrl());
                uploadImage.setImageToken(voteOption.get(i).getOptionImage(j).getToken());
                list.add(uploadImage);
            }
            newVote.setUploadImages(list);
            newVoteLists.add(newVote);
        }
        return newVoteLists;
    }

    public static String onDescribe(List<PbBean.PbBaseResDescription> description) {
        String descriptions = "";
        for(int i = 0; i < description.size(); i++) {
            if(description.get(i).getDescType() == PbBean.PbSignBaseDescriptionType.DESCRIPTION_TPYE_TEXT) {
                descriptions = descriptions + description.get(i).getDescText();

            } else {
                String tagPath = "<img src=\"" + description.get(i).getDescImage().getUrl() + "\"/>";
                descriptions = descriptions + tagPath;
            }
        }
        return descriptions;
    }

    public static List<EditDescribe> onEditDescribeList(List<PbBean.PbBaseResDescription> description) {
        List<EditDescribe> list = new ArrayList<>();
        for(int i = 0; i < description.size(); i++) {
            EditDescribe editDescribe = new EditDescribe();
            if(description.get(i).getDescType() == PbBean.PbSignBaseDescriptionType.DESCRIPTION_TPYE_TEXT) {
                editDescribe.setDescribe(0);
                editDescribe.setmName(description.get(i).getDescText());
                list.add(editDescribe);
            } else {
                editDescribe.setDescribe(1);
                editDescribe.setmUrl(description.get(i).getDescImage().getUrl());
                editDescribe.setToken(description.get(i).getDescImage().getToken());
                list.add(editDescribe);
            }
        }
        return list;
    }
    public static List<String> onImageList(List<PbBean.PbBaseResDescription> description) {
        List<String> list = new ArrayList<>();
        for(int i = 0; i < description.size(); i++) {
            if(description.get(i).getDescType() == PbBean.PbSignBaseDescriptionType.DESCRIPTION_TPYE_IMG) {
                list.add(description.get(i).getDescImage().getUrl());
            }
        }
        return list;
    }

    public static Map<String, String> onMapURLToken(List<PbBean.PbBaseResDescription> description) {
        Map<String, String> map = new HashMap<>();
        for(int i = 0; i < description.size(); i++) {
            if(description.get(i).getDescType() == PbBean.PbSignBaseDescriptionType.DESCRIPTION_TPYE_IMG) {
                map.put(description.get(i).getDescImage().getUrl(), description.get(i).getDescImage().getToken());
            }
        }
        return map;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK) {
            FragmentManager manager = getSupportFragmentManager();
            int count = manager.getBackStackEntryCount();
            if(count == 0) {
                NewProblems.getInstance().deleteNewProblems();
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    protected String getTopTitle() {
        return getString(R.string.edit_problems);
    }
    @Override
    protected BaseFragment onCreateContentFragment() {
        return new EditProblemsFragment();
    }
    @Override
    protected void setupTitleView() {
        setOperateTitle(getString(R.string.submit_problems));
    }
    @Override
    public boolean shouldShowBack() {
        return false;
    }
    @Override
    protected boolean shouldShowQuestionBack() {
        return true;
    }
    @Override
    protected Bundle onPutArguments() {
        Bundle args = new Bundle();
        args.putSerializable("NewProblems", getIntent().getSerializableExtra("NewProblems"));
        return args;
    }
    @Override
    public void onHeadBackClick(View view) {
        //        EventBusUtil.post(new MessageNoticeEvent(MessageNoticeEvent.READ_ALL_MESSAGE));
        UdaUtils.hideInputMethod(this, view);
        FragmentManager manager = getSupportFragmentManager();
        int count = manager.getBackStackEntryCount();
        if(count == 0) {
            NewProblems.getInstance().deleteNewProblems();
            finish();
        } else {
            try {
                manager.popBackStackImmediate();
            } catch(IllegalStateException ignored) {
            }
        }
    }

    @Override
    public void onHeadActionClick(View view) {
        //        EventBusUtil.post(new MessageNoticeEvent(MessageNoticeEvent.READ_ALL_MESSAGE));
        EventBusUtil.post(new NewProblemsEvent(NewProblemsEvent.EVENT_COMMENT_PUBLISHED));
    }
}
